package com.zuul.filter

import com.netflix.zuul.ZuulFilter
import com.netflix.zuul.context.RequestContext
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import java.nio.charset.StandardCharsets

@Component
class MainFilter : ZuulFilter() {
    @Value("\${server.port}")
    lateinit var port: String

    override fun run(): Any? {
        println("======================= ZuulFilter,port:$port =======================")

        val context = RequestContext.getCurrentContext()
        val request = context.request
        val response = context.response
        request.characterEncoding = StandardCharsets.UTF_8.name()
        response.characterEncoding = StandardCharsets.UTF_8.name()

//        val zuul_param = request.getParameter("zuul_param")
//        if (zuul_param != "YFree") {
//            context.setSendZuulResponse(false)
//            context.responseBody = "zuul_param不正确"
//            context.responseStatusCode = 401
//            return null
//        }
        return null
    }

    override fun shouldFilter(): Boolean {
        return true
    }

    override fun filterType(): String {
        return "pre" //pre,post,route,error
    }

    override fun filterOrder(): Int {
        return 0
    }
}
